package cmd

import (
	"context"
	"sync"
	"video-api/utility"

	"github.com/gogf/gf/os/glog"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcmd"
)

var (
	serverWg = sync.WaitGroup{}

	Main = &gcmd.Command{
		Name:  "short_drama",
		Usage: "short_drama",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			var allServers = []*gcmd.Command{Prometheus, Http, Asynqmon}
			for _, server := range allServers {
				serverWg.Add(1)
				cmd := server
				//使用g.Go发生panic不会影响其他服务
				g.Go(ctx, func(ctx context.Context) {
					if err := cmd.Func(ctx, parser); err != nil {
						g.Log().Fatalf(ctx, "%v start fail:%v", cmd.Name, err)
					}
				}, func(ctx context.Context, err error) {
					var level = glog.LEVEL_ERRO
					utility.Logf(level, ctx, "server exec failed:%+v", err)
				})
			}
			serverWg.Wait()
			return
		},
	}
)

func init() {
	if err := Main.AddCommand(Prometheus, Http, Asynqmon); err != nil {
		panic(err)
	}
}
